package com.rahms.server.controller;

import com.rahms.common.constant.MessageConstant;
import com.rahms.common.result.R;
import com.rahms.pojo.entity.Medication;
import com.rahms.server.service.MedicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 药品信息控制器
 */
@RestController
@RequestMapping("/medication")
public class MedicationController {
    
    @Autowired
    private MedicationService medicationService;
    
    /**
     * 获取用户的药品列表
     */
    @GetMapping("/list")
    public R<List<Medication>> getMedicationList(@RequestParam Long userId) {
        List<Medication> list = medicationService.getMedicationListByUserId(userId);
        return R.success(list);
    }
    
    /**
     * 搜索药品
     */
    @GetMapping("/search")
    public R<List<Medication>> searchMedications(@RequestParam Long userId,
                                               @RequestParam String keyword) {
        List<Medication> list = medicationService.searchMedications(userId, keyword);
        return R.success(list);
    }
    
    /**
     * 获取药品详情
     */
    @GetMapping("/detail")
    public R<Medication> getMedicationDetail(@RequestParam Long medicationId) {
        Medication medication = medicationService.getById(medicationId);
        return medication != null ? R.success(medication) : R.error(MessageConstant.MEDICATION_NOT_EXIST);
    }
    
    /**
     * 添加药品信息
     */
    @PostMapping("/add")
    public R<Boolean> addMedication(@RequestBody Medication medication) {
        boolean result = medicationService.save(medication);
        return result ? R.success(true).add("message", MessageConstant.MEDICATION_ADD_SUCCESS) : R.error(MessageConstant.SAVE_FAILED);
    }
    
    /**
     * 更新药品信息
     */
    @PostMapping("/update")
    public R<Boolean> updateMedication(@RequestBody Medication medication) {
        boolean result = medicationService.updateById(medication);
        return result ? R.success(true).add("message", MessageConstant.MEDICATION_UPDATE_SUCCESS) : R.error(MessageConstant.UPDATE_FAILED);
    }
    
    /**
     * 删除药品信息
     */
    @PostMapping("/delete")
    public R<Boolean> deleteMedication(@RequestParam Long medicationId) {
        boolean result = medicationService.removeById(medicationId);
        return result ? R.success(true).add("message", MessageConstant.MEDICATION_DELETE_SUCCESS) : R.error(MessageConstant.DELETE_FAILED);
    }
}